import groovy.xml.MarkupBuilder
import groovy.xml.XmlUtil
import groovy.xml.StreamingMarkupBuilder
import scriptlerweb.Provider
import scriptlerweb.Script



def action = params?.action

switch(action){
	/*
	case 'delall':
		def providers = dao.ofy().query(Provider.class)
		providers.each{ p ->
			log.info "delete: $p"
			dao.ofy().delete(p)
		}
	
		forward '/providerdetails.gtpl'
	
	
		break
	*/
	case 'show':
		if(!params.id){
			request.setAttribute 'msg', "no provider id passed!"
			forward '/failure.gtpl'
		}else{
			def provider = dao.loadProvider(params.id)
			def scripts = dao.loadScriptsByProviderId(params.id)
			request.setAttribute 'provider', provider
			request.setAttribute 'scripts', scripts
			
			forward '/providerdetails.gtpl'
		}
	
		break
	case 'edit':
		def provider = dao.loadCurrentProvider(user)
		if(!provider){
			request.setAttribute 'msg', "you'r not a valid provider yet, please sign in and provide a script..."
			forward '/failure.gtpl'
		}else{
			request.setAttribute 'provider', provider
			forward '/provideredit.gtpl'
		}
	
		break
	case 'update':
	
		def provider = dao.loadProvider(params.providerId)
		log.fine "update: provider $provider"
	
		if(user && provider && provider.accountUserId){
			if(user.getUserId().equals(provider.accountUserId)){
				if(provider){
					provider.name = params?.name
					provider.comment = params?.comment
					provider.url = params?.url
					if(!provider.name){
						// ensure a provider has a name!
						request.setAttribute 'msg', "name is mandatory!"
						request.setAttribute 'provider', provider
						forward '/provideredit.gtpl'
					}else if(provider.url && !provider.url.startsWith('http')){
						// ensure a provider has a name!
						request.setAttribute 'msg', "url must start with 'http://' or 'https://'!"
						request.setAttribute 'provider', provider
						forward '/provideredit.gtpl'
					}else{
						def verifyProvider = dao.loadProviderByName(provider.name)
						if(verifyProvider && verifyProvider.id != provider.id){
							request.setAttribute 'msg', "name '${provider.name}' already taken!"
							request.setAttribute 'provider', provider
							forward '/provideredit.gtpl'
						}else{
							dao.ofy().put(provider)
							log.fine "updated provider ${provider.id}"
							redirect "/provider/show/${provider.id}"
						}
					}
				}else{
					request.setAttribute 'msg', "could not load provider details!"
					forward "/provider/list"
				}
			}else{
				request.setAttribute 'msg', "your not allowed to edit this provider!"
				forward "/provider/show/${provider.id}"
			}
		}else{
			request.setAttribute 'msg', "not ready! user: $user, provider: $provider"
			forward "/provider/show/${provider.id}"
		}
	
	
		break
	default: 
		def orderBy = params.orderBy ? params.orderBy : "name"
		def providers = dao.ofy().query(Provider.class).order(orderBy).list()
	
		request.setAttribute 'providers', providers
	
		log.fine "Forwarding to the catalog template"
	
	// switch the order for the next link rendering in the template
	// To specify that the order ought to be in descending order, precede the name with a hyphen (-). Without a hyphen, the order is ascending.
		if(orderBy.startsWith("-")){
			request.setAttribute 'order', ""
		}else{
			request.setAttribute 'order', "-"
		}
		forward '/providerlist.gtpl'
}

